<?php

namespace App\Jobs;

use App\Services\TracerFactory;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use OpenTracing\Reference;
use OpenTracing\StartSpanOptions;
use OpenTracing\Tracer;
use const OpenTracing\Formats\TEXT_MAP;

class TraceJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable;

    /**
     * @var array
     */
    private $carrier;
    /**
     * @var Tracer
     */
    private $tracer;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(array $carrier)
    {
        $this->carrier = $carrier;
    }

    public function carrier(array $carrier)
    {
        $this->carrier = $carrier;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $tracer = TracerFactory::make();
        $spanContext = $tracer->extract(TEXT_MAP, $this->carrier);
        $parent = $tracer->startSpan('job test', ['child_of' => $spanContext]);

        $options = StartSpanOptions::create(['references' => Reference::createForSpan(Reference::FOLLOWS_FROM, $parent)]);
        $span = $tracer->startSpan('ttt', $options);
        $parent->finish();
        $span->finish();
        $tracer->flush();

        dump('job test');
    }
}
